'use strict';

    angular.module('app')
        .controller('dmAuthUpdateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
        function($scope, $rootScope, $state, $timeout, toaster) {
        $scope.record = {}
        if($state.includes('**.doorSysManager.authManager.update')){
        	$scope.loading = true;
        	$scope.reset = false;
            $scope.title="修改授权";
            var id = $state.params.id;
            activate(id);
        }else if($state.includes('**.doorSysManager.authManager.create')){
            $scope.title="新增授权";
            $scope.reset = true;
            setTimeout(function(){
                $scope.myCroppedImage = defaultAva;
                !$rootScope.$$phase && $scope.$apply();
            },300);
        }
        validate();
       $scope.resetFrom = function(){
       		$scope.record = {}
       }
        $scope.selUser = function(){
        	$scope.record.personId=$scope.record.person.split('_')[0];
        	$scope.record.cardNo=$scope.record.person.split('_')[1];
        }

        $scope.submit= function(){
            var m = $scope.record;
            if(m){
            	$scope.record.status = '0';
                $scope.isDisabled = true;//提交disabled
                $.ajax({
    				type: 'POST',
    	            dataType: 'json',
    				contentType:'application/json;charset=UTF-8',
    				url : '/doorSysManager',
    				data: angular.toJson($scope.record)
    			}).then(function(result){
                    if(result.httpCode ==200){//成功
                        toaster.clear('*');
                        toaster.pop('success', '', "保存成功");
                        $timeout(function(){
                            $state.go('main.doorSysManager.authManager.list');
                        },2000);
                    }else{
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                        $scope.isDisabled = false;
                    }
                });
            }
        }

        // 初始化页面
        function activate(id) {
	        $scope.loading = true;
        	$.ajax({
        		type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/menu/read/detail',
				data: angular.toJson({'id': id})
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.record = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
        }

        //表单验证
        function validate(userId){
            jQuery('form').validate({
//                rules: {
//                	deptName: {
//                        required: true,
//                        stringCheck:[],
//                        maxLengthB:[20]
//                    },
//                    sortNo: {
//                        required: true
//                    }
//                },
//                messages: {
//                	deptName: {
//                        required: '请填写部门名称',
//                        maxLengthB:"部门名称不得超过{0}个字符"
//                    },
//                    sortNo: {
//                        required: '请填写排序'
//                    }
//                },
                submitHandler: function() {
                    $scope.submit();
                }
            });
        }

    }]);